﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;

namespace ProjectBuilder
{
    public enum XsltTemplates
    {
        DetailDescription,
        SourceCode
    }
    public static class XmlNodeExtension
    {
        private static Dictionary<string, XslTransform> _listXsl = new Dictionary<string,XslTransform>();

        public static XslTransform getXsltTransform(string fileName)
        {
            if (!_listXsl.ContainsKey(fileName))
            {
                var xsl = new XslTransform();
                xsl.Load(fileName);
                _listXsl.Add(fileName, xsl);
            }
            return _listXsl[fileName];
        }

        public static string getTemplate(this XsltTemplates file)
        {
            switch (file)
            {
                case XsltTemplates.DetailDescription:
                    return "templates/detaildescription.xslt";
                case XsltTemplates.SourceCode:
                    return "templates/source.xslt";
            }
            return string.Empty;
        }

        public static string ToString(this XmlNode[] node, XsltTemplates fileName)
        {
            return node.ToString(fileName.getTemplate());
        }

        public static string ToString(this XmlNode[] node, string sFileName)
        {
            if (node == null) return "";
            var str = "";
            foreach (var n in node)
            {
                str += n.ToString(sFileName) + Environment.NewLine;
            }
            return str;
        }

        public static string ToString(this XmlNode node, XsltTemplates fileName)
        {
            return node.ToString(fileName.getTemplate());
        }

        public static string ToString(this XmlNode node, string sFileName)
        {
            if (node == null) return "";

            var p = "<?xml version=\"1.0\"?>" + Environment.NewLine + "<root>" + node.OuterXml + "</root>";

            if (!Directory.Exists("files"))
            {
                Directory.CreateDirectory("files");
            }
            return p.Transform(sFileName);
        }

        public static string Transform(this string sNode, string sFileName)
        {
            var myXPathDoc = new XPathDocument(new StringReader(sNode));

            XsltArgumentList xslArgs = null;//new XsltArgumentList();

            //create custom object
            //CustomDate custDate = new CustomDate();

            //pass an instance of the custom object
            //xslArgs.AddExtensionObject("urn:custDate", custDate);

            var f = new MemoryStream();
            var myWriter = new XmlTextWriter(f, Encoding.UTF8);

            getXsltTransform(sFileName).Transform(myXPathDoc, xslArgs, myWriter);

            myWriter.Flush();

            f.Flush();
            f.Position = 0;

            var sr = new StreamReader(f);
            var s= sr.ReadToEnd();

            myWriter.Close();
            return s.Replace("\xA0", "&nbsp;");
        }
    }
}
